package com.cndw.kungfu.core;

import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cndw.kungfu.core.cache.CacheDisciple;
import com.cndw.kungfu.core.cache.CacheGoods;
import com.cndw.kungfu.core.cache.CacheLottery;
import com.cndw.kungfu.core.cache.CacheMonster;
import com.cndw.kungfu.core.cache.CacheNpc;
import com.cndw.kungfu.core.cache.CachePendant;
import com.cndw.kungfu.core.cache.CachePet;
import com.cndw.kungfu.core.cache.CacheQuiz;
import com.cndw.kungfu.core.cache.CacheScene;
import com.cndw.kungfu.core.cache.CacheSkill;
import com.cndw.kungfu.core.cache.CacheStory;
import com.cndw.kungfu.core.cache.CacheTask;
import com.cndw.kungfu.core.cache.CacheTitle;
import com.cndw.kungfu.core.cache.CacheUndercity;
import com.cndw.kungfu.core.cache.CacheUnion;
import com.cndw.kungfu.core.cache.CacheUpgrade;
import com.cndw.kungfu.domain.BuffMode;
import com.cndw.kungfu.domain.GoodsExt;
import com.cndw.kungfu.domain.GoodsMode;
import com.cndw.kungfu.domain.GoodsSuite;
import com.cndw.kungfu.domain.GoodsType;
import com.cndw.kungfu.domain.LevelUpgrade;
import com.cndw.kungfu.domain.Monster;
import com.cndw.kungfu.domain.MonsterDeploy;
import com.cndw.kungfu.domain.Npc;
import com.cndw.kungfu.domain.Pendant;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.domain.QuizQuestion;
import com.cndw.kungfu.domain.Scene;
import com.cndw.kungfu.domain.SkillMode;
import com.cndw.kungfu.domain.Story;
import com.cndw.kungfu.domain.TaskMode;
import com.cndw.kungfu.domain.Title;
import com.cndw.kungfu.domain.Undercity;
import com.cndw.kungfu.domain.UnionConfig;
import com.cndw.kungfu.domain.UnionSkillConfig;
import com.cndw.kungfu.model.disciple.domain.DiscipleMode;
import com.cndw.kungfu.model.player.PlayerManager;
import com.cndw.kungfu.model.task.PlayerTasks;
import com.cndw.kungfu.model.task.TaskManager;

/**
 * 系统数据集合 数据集合应当在系统启动时候一并加载
 * 
 * @author fantadust
 */
@Component
public class CacheCenter {
	private static ConcurrentHashMap<Long, Player> PLAYERSMAP = new ConcurrentHashMap<Long, Player>();
	private static ConcurrentHashMap<String, Long> playerIdsMap = new ConcurrentHashMap<String, Long>();
	private static ConcurrentHashMap<Long, PlayerTasks> PLAYERTASKSMAP = new ConcurrentHashMap<Long, PlayerTasks>();
	@Autowired
	private PlayerManager playerManager;

	@Autowired
	private CacheDisciple cacheDisciple;
	@Autowired
	private CacheGoods cacheGoods;
	@Autowired
	private CacheMonster cacheMonster;
	@Autowired
	private CacheNpc cacheNpc;
	@Autowired
	private CachePendant cachePendant;
	@Autowired
	private CacheScene cacheScene;
	@Autowired
	private CacheSkill cacheSkill;
	@Autowired
	private CacheStory cacheStory;
	@Autowired
	private CacheTask cacheTask;
	@Autowired
	private CacheUndercity cacheUndercity;
	@Autowired
	private CacheUnion cacheUnion;
	@Autowired
	private CacheUpgrade cacheUpgrade;
	@Autowired
	private CacheQuiz cacheQuiz;
	@Autowired
	private CacheTitle cacheTitle;
	@Autowired
	private CachePet cachePet;
	@Autowired
	private CacheLottery cacheLottery;
	@Autowired
	private TaskManager taskManager;

	public void cacheInit() {
		synchronized (CacheCenter.class) {
			cacheDisciple.init();// 1
			cacheGoods.init();// 2
			cacheMonster.init();// 3
			cacheNpc.init();// 4
			cachePendant.init();// 5
			cacheScene.init();// 6
			cacheSkill.init();// 7
			cacheStory.init();// 8
			cacheTask.init();// 9
			cacheUndercity.init();// 10
			cacheUnion.init();// 11
			cacheUpgrade.init();// 12
			cacheQuiz.init();// 13
			cacheTitle.init();// 14
			cachePet.init();// 15
			cacheLottery.init();// 16
		}
	}

	public Collection<Npc> getAllNpcs() {
		return cacheNpc.getAll();
	}

	public Collection<Scene> getAllScenes() {
		return cacheScene.getAll();
	}

	public BuffMode getBuffMode(int buffId) {
		return cacheSkill.getBuffMode(buffId);
	}

	public Collection<BuffMode> getBuffModes() {
		return cacheSkill.getBuffModes();
	}

	/**
	 * 徒弟
	 * 
	 * @param discipleId
	 * @return
	 */
	public DiscipleMode getDiscipleMode(int discipleId) {
		return cacheDisciple.getOne(discipleId);
	}

	public Collection<DiscipleMode> getDiscipleModes() {
		return cacheDisciple.getAll();
	}

	public Map<Integer, GoodsExt> getGoodsExt(int goodsId) {
		return cacheGoods.getGoodsExt(goodsId);
	}

	public GoodsExt getGoodsExtByLevel(int goodsId, int level) {
		Map<Integer, GoodsExt> map = getGoodsExt(goodsId);
		if (map == null) {
			return null;
		}
		return map.get(level);
	}

	public Collection<GoodsMode> getGoodsMode() {
		return cacheGoods.getAll();
	}

	/**
	 * 获取一个物品类
	 * 
	 * @param goodsId
	 * @return
	 */
	public GoodsMode getGoodsModeById(int goodsId) {
		return cacheGoods.getOne(goodsId);
	}

	/**
	 * 根据套装id获取套装对象
	 * 
	 * @param level
	 *            帮派等级
	 * @return 返回帮派等级配置对象
	 */
	public GoodsSuite getGoodsSuiteById(int Id) {
		return cacheGoods.getgGoodsSuite(Id);
	}

	public GoodsType getGoodsTypeByGoodTypeId(int goodTypeId) {
		return cacheGoods.getGoodsType(goodTypeId);
	}

	/**
	 * 获取升级经验信息
	 * 
	 * @return
	 */
	public LevelUpgrade getLevelUpgrade(int level) {
		return cacheUpgrade.getOne(level);
	}

	/**
	 * 获取怪物模型
	 * 
	 * @param id
	 * @return
	 */
	public Monster getMonsterById(int id) {
		return cacheMonster.getOne(id);
	}

	public MonsterDeploy getMonsterDeployById(int deployId) {
		return cacheMonster.getDeploy(deployId);
	}

	public List<MonsterDeploy> getMonsterDeploysBySceneId(int sceneId) {
		return cacheMonster.getDeploys(sceneId);
	}

	/**
	 * 获取某个npc对象
	 * 
	 * @param id
	 * @return
	 */
	public Npc getNpcById(int id) {
		return cacheNpc.getOne(id);
	}

	/**
	 * 获取全部任务
	 * 
	 * @return
	 */
	public Collection<TaskMode> getNpcTasks() {
		return cacheTask.getAll();
	}

	/**
	 * 全部任务
	 */
	public Pendant getPendant(int id) {
		return cachePendant.getOne(id);
	}

	public List<Pendant> getPendantBySceneId(int sceneId) {
		return cachePendant.getPendantBySceneId(sceneId);
	}

	/**
	 * 从集合里面读取player对象
	 * 
	 * @param playerId
	 * @return
	 */
	public Player getPlayerByPlayerId(long playerId) {
		if (!PLAYERSMAP.containsKey(playerId)) {
			Player player = playerManager.getByPlayerIdFromDb(playerId);
			putPlayer(player);
		}
		return PLAYERSMAP.get(playerId);
	}
	public long getPlayerIdByAcc(String acc){
		if (!playerIdsMap.containsKey(acc)) {
			return playerManager.getPlayerIdByAccountName(acc);
		}
		return playerIdsMap.get(acc);
	}
	

	public Collection<Player> getPlayers() {
		return PLAYERSMAP.values();
	}

	/**
	 * 回收用户内存数据
	 */
	public void gcPlayer(long playerId) {
		PLAYERSMAP.remove(playerId);
		PLAYERTASKSMAP.remove(playerId);
	}

	public void putPlayer(Player player) {
		if (player != null && player.getPlayerId() > 0) {
			PLAYERSMAP.putIfAbsent(player.getPlayerId(), player);
			playerIdsMap.putIfAbsent(player.getAccountName(), player.getPlayerId());
		}
	}

	/**
	 * 读取角色任务集合
	 * 
	 * @param playerId
	 * @return
	 */
	public PlayerTasks getPlayerTasksByPlayerId(long playerId) {
		if (playerId == 0) {
			return null;
		}
		if (PLAYERTASKSMAP.containsKey(playerId) == false) {
			PlayerTasks playerTasks = taskManager.initPlayerTasks(playerId);
			if (playerTasks != null) {
				PLAYERTASKSMAP.putIfAbsent(playerId, playerTasks);
			}
		}
		return PLAYERTASKSMAP.get(playerId);
	}

	/**
	 * 获取一个场景信息
	 * 
	 * @param sceneId
	 * @return
	 */
	public Scene getSceneById(int sceneId) {
		return cacheScene.getOne(sceneId);
	}

	public LinkedHashSet<Integer> getShopGoodsMap() {
		return cacheGoods.getShopIds();
	}

	public SkillMode getSkillMode(int skillid) {
		return cacheSkill.getOne(skillid);
	}

	public Collection<SkillMode> getSkillModes() {
		return cacheSkill.getAll();
	}

	/**
	 * 获取一个剧情
	 * 
	 * @param id
	 * @return
	 */
	public Story getStory(int id) {
		return cacheStory.getOne(id);
	}

	public TaskMode getTaskModeById(int taskId) {
		return cacheTask.getOne(taskId);
	}

	/**
	 * 获取地下城模型
	 * 
	 * @param id
	 * @return
	 */
	public Undercity getUndercity(int undercityId) {
		return cacheUndercity.getOne(undercityId);
	}

	/**
	 * 获取帮派等级配置数据
	 * 
	 * @param level
	 *            帮派等级
	 * @return 返回帮派等级配置对象
	 */
	public UnionConfig getUnionConfigByLevel(int level) {
		return cacheUnion.getOne(level);
	}

	/**
	 * 获取帮派节能配置数据
	 * 
	 * @param level
	 *            帮派等级
	 * @return 返回帮派等级配置对象
	 */
	public UnionSkillConfig getUnionSkillConfig(String key) {
		return cacheUnion.getUnionSkillConfig(key);
	}

	public Collection<Undercity> getuUndercities() {
		return cacheUndercity.getAll();
	}

	/**
	 * 通过id获得问题
	 * 
	 * @param id
	 *            问题id
	 * @return
	 */
	public final QuizQuestion getQuestionById(int id) {
		return cacheQuiz.getOne(id);
	}

	/**
	 * 通过难度值获得问题
	 * 
	 * @param diff
	 *            难度
	 * @return
	 */
	public final QuizQuestion getQuestionByDifficulty(int diff, int type) {
		return cacheQuiz.getOneByDifficulty(diff, type);
	}

	/**
	 * 获取称号
	 */
	public final Title getTitle(int titleId) {
		return cacheTitle.getOne(titleId);
	}

	/**
	 * 根据获得需求得到称号，从小到大排序
	 * 
	 * @param type
	 * @return
	 */
	public final List<Title> getTitles(int type) {
		return cacheTitle.getTitles(type);
	}

	/**
	 * 获取全部称号
	 */
	public final Collection<Title> getAllTitles() {
		return cacheTitle.getAll();
	}

	/**
	 * 根据类型和条件获取称号
	 */
	public final Title getTitle(int type, int require) {
		return cacheTitle.getTitle(type, 0, require);
	}

	/**
	 * 清理下缓存
	 */
	public void updateCache() {

		synchronized (CacheCenter.class) {
			cacheDisciple.remove();
			cacheGoods.remove();
			cacheMonster.remove();
			cacheNpc.remove();
			cachePendant.remove();
			cacheScene.remove();
			cacheSkill.remove();
			cacheStory.remove();
			cacheTask.remove();
			cacheUndercity.remove();
			cacheUnion.remove();
			cacheUpgrade.remove();
			cacheQuiz.remove();
			cacheTitle.remove();
			cachePet.remove();
			cacheLottery.remove();
		}
	}
}
